package com.situ.springboot.interceptor;

import com.situ.springboot.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //所有的请求会首先经过拦截器，在拦截器里面判断session里面有没有user
        //如果有代表已经登录
        //如果没有代表没有登录，跳转到登录界面
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            response.sendRedirect("/user/toLogin");
            return  false;
        }

        //已经登录,可以访问后面资源
        return true;
    }
}
